<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智习伴 - 登录</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css">
    <link rel="stylesheet" href="static/css/style.css">
    <style>
        .role-option {
            transition: all 0.3s ease;
        }
        .role-option.selected {
            border-color: #3b82f6;
            background-color: #eff6ff;
            box-shadow: 0 4px 6px rgba(59, 130, 246, 0.1);
        }
        .role-option:hover {
            background-color: #f9fafb;
        }
    </style>
</head>
<body class="login-bg">
    <div class="login-card w-full max-w-md mx-4">
        <div class="p-8">
            <div class="text-center mb-8">
                <h1 class="text-3xl font-bold text-gray-800 mb-2">智习伴</h1>
                <p class="text-gray-600">欢迎回到您的学习空间</p>
            </div>
            
            <form id="loginForm">
                <div class="mb-6">
                    <label class="block text-gray-700 text-sm font-medium mb-2">选择角色</label>
                    <div class="grid grid-cols-2 gap-4">
                        <div class="role-option border-2 border-gray-200 rounded-lg p-4 cursor-pointer selected" data-role="student">
                            <div class="text-center">
                                <div class="text-2xl mb-2">👨‍🎓</div>
                                <div class="font-medium text-gray-800">学生</div>
                            </div>
                        </div>
                        <div class="role-option border-2 border-gray-200 rounded-lg p-4 cursor-pointer" data-role="teacher">
                            <div class="text-center">
                                <div class="text-2xl mb-2">👩‍🏫</div>
                                <div class="font-medium text-gray-800">教师</div>
                            </div>
                        </div>
                    </div>
                    <input type="hidden" id="role" name="role" value="student">
                </div>
                
                <div class="mb-6">
                    <label for="username" class="block text-gray-700 text-sm font-medium mb-2">用户名</label>
                    <input type="text" id="username" class="form-input w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500" placeholder="请输入用户名" required>
                </div>
                
                <div class="mb-6">
                    <label for="password" class="block text-gray-700 text-sm font-medium mb-2">密码</label>
                    <input type="password" id="password" class="form-input w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500" placeholder="请输入密码" required>
                </div>
                
                <div class="flex items-center justify-between mb-6">
                    <label class="flex items-center">
                        <input type="checkbox" class="form-checkbox rounded text-blue-500">
                        <span class="ml-2 text-gray-700 text-sm">记住我</span>
                    </label>
                    <a href="#" class="text-sm text-blue-500 hover:text-blue-700">忘记密码？</a>
                </div>
                
                <button type="submit" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 px-4 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2">
                    登录
                </button>
            </form>
            
            <div class="mt-6 text-center">
                <p class="text-gray-600 text-sm">
                    还没有账户？
                    <a href="register.html" class="text-blue-500 hover:text-blue-700 font-medium">立即注册</a>
                </p>
            </div>
        </div>
    </div>

    <script>
        // API基础URL
        const API_BASE_URL = 'http://localhost:5000/api';
        
        // 角色选择功能
        document.querySelectorAll('.role-option').forEach(option => {
            option.addEventListener('click', function() {
                // 移除所有选中状态
                document.querySelectorAll('.role-option').forEach(opt => {
                    opt.classList.remove('selected');
                });
                
                // 添加选中状态到当前选项
                this.classList.add('selected');
                
                // 更新隐藏字段的值
                document.getElementById('role').value = this.dataset.role;
            });
        });
        
        // 处理登录
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value;
            const role = document.getElementById('role').value;
            
            // 前端验证
            if (!username || !password) {
                alert('请填写用户名和密码');
                return;
            }
            
            try {
                const response = await fetch(`${API_BASE_URL}/auth/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ username, password })
                });
                
                const data = await response.json();
                
                if (response.ok) {
                    // 保存用户信息到本地存储
                    localStorage.setItem('token', data.access_token);
                    localStorage.setItem('user', JSON.stringify(data.user));
                    
                    // 根据角色跳转到不同页面
                    if (data.user.role === 'teacher') {
                        window.location.href = 'teacher.html';
                    } else {
                        window.location.href = 'student.html';
                    }
                } else {
                    alert('登录失败: ' + data.error);
                }
            } catch (error) {
                alert('网络错误，请稍后重试');
                console.error('登录错误:', error);
            }
        });
    </script>
</body>
</html>